//标签函数库
var nowtag,nowdiv,lastdiv; //页面标签全局变量,包括当前标签,当前页面,前一个页面
var taggroup = "firstpage";//标签点击顺序记忆数组字符串
var explorer = getOs();//浏览器属性
var hidded = false;//树的隐藏属性
var searchhidded = false;//搜索框隐藏属性
//var atid,level,first;//初始化参数
//Onload函数
function sf(id,level,first){
	setTimeout("dosf('"+id+"','"+level+"','"+first+"')",1000);
}
function dosf(id,level,first){
	getValue(id,document.getElementById('tree'),level,first);
}
//alert(atid);
//增加标签和页面
function addtag(id,inner,pic){
	var hid=id+'tag';
	if(document.getElementById(hid)==null){
		var h=CreateEl("span",hid,"selectag");
		var hw=inner.length;
		h.innerHTML="<img src=\"/javascripts/dep_manage/images/"+pic+"\" align=\"absmiddle\">&nbsp;"+inner;
		if(explorer==2){
			h.style.width=36+12*hw+18;
			h.style.maxWidth=36+12*hw+18;
		}else{
			h.style.width=36+12*hw+18;
			h.style.maxWidth=36+12*hw+18;
		}
		document.getElementById("tag").appendChild(h);
		document.getElementById(hid).onclick= new Function("switchtag('"+id+"')");
	}
	if(document.getElementById(id)==null){
		var l=CreateEl("div",id,"newLayer");
		document.getElementById("detail").appendChild(l);
	}
	switchtag(id);
}

//交换标签
function switchtag(id){
	if(nowdiv!=id){
		var hid=id+'tag';

		document.getElementById(id).style.display="block";
		var st = document.getElementById(hid);
		st.className="selectag";
		document.getElementById("tablist_l").innerHTML=st.innerHTML+"<img src=\"/javascripts/dep_manage/images/tablist_up.gif\" align=\"absmiddle\">";
		if(st.id!="firstpagetag"){
			st.innerHTML=st.innerHTML+"<div id=close_tab>&nbsp;<img src=\"/javascripts/dep_manage/images/close_tab.gif\"></div>";
			st.getElementsByTagName("img")[1].onclick= new Function("removetag('"+id+"')");
		}
		addtaggroup(id);
		hidenow(id);
		adjusttagwidth();
	}
	initDe(id);
}

//隐藏当前标签和页面
function hidenow(id){
	var hid=id+'tag';
	if(nowdiv!=null&&nowdiv!=id){
		document.getElementById(nowdiv).style.display="none";
	}
	if(nowtag!=null&nowtag!=hid){
		var nt = document.getElementById(nowtag);
		nt.className="newtag";
		if(nowtag!="firstpagetag"){
			nt.removeChild(nt.getElementsByTagName("div")[0]);
			//alert(nt.clientWidth);
			nt.style.width=nt.clientWidth - 18;
			//alert(nt.clientWidth);
		}
	}
	nowtag=hid;
	nowdiv=id;
}

//删除标签和页面
function removetag(id){
	var hid=id+"tag";
	getlastdiv(id);
	switchtag(lastdiv);
	document.getElementById("tag").removeChild(document.getElementById(hid));
	document.getElementById("detail").removeChild(document.getElementById(id));
	//alert(lastdiv);

	adjusttagwidth();
}

//调整标签宽度
function adjusttagwidth(){
	//alert(nowtag);
	var nt = document.getElementById(nowtag);
	var tl = document.getElementById("tablist");
	if(nowtag!="firstpagetag"){
		if(explorer==2){
			tl.style.width = parseInt(nt.style.maxWidth)-8;
		}else{
			tl.style.width = parseInt(nt.style.maxWidth)-8;
		}
	}else{
		tl.style.width = 70;
	}
	if(nowtag!="firstpagetag"){
		thentmaxwidth = parseInt(nt.style.maxWidth);
	}else{
		thentmaxwidth = -1;
	}

	if (!hidded){
		var detailwidth = document.body.clientWidth-thentmaxwidth-232-61;
	}else{
		var detailwidth = document.body.clientWidth-thentmaxwidth-12-61;
	}
	var tags = document.getElementById("tag").getElementsByTagName("span");
	var tagwidth = 0;
	for(var i = 1;i<tags.length;i++){
		if(tags[i].id!=nowtag){
			tagwidth += parseInt(tags[i].style.maxWidth)+1-18;
		}
	}

	if(tagwidth>detailwidth){
		var thewidth = tagwidth-detailwidth;
		var minuswidth;

		for(i = 1;i<tags.length;i++){
			if(tags[i].id!=nowtag){
				var twidth = parseInt(tags[i].style.maxWidth) - 18;
				var tm = thewidth*twidth/tagwidth;
				if (tm-parseInt(tm)>0.4){
					minuswidth = parseInt(tm)+2;
				}else{
					minuswidth = parseInt(tm)+1;
				}
				tags[i].style.width=parseInt(tags[i].style.maxWidth)-minuswidth-18;
				tags[i].title=tags[i].innerText;
			}else{
				tags[i].style.width=parseInt(tags[i].style.maxWidth);
				tags[i].title="";
			}
		}
	}else{
		for(i = 1;i<tags.length;i++){
			if(tags[i].id!=nowtag){
				tags[i].style.width=parseInt(tags[i].style.maxWidth)-18;
				tags[i].title="";
			}else{
				tags[i].style.width=parseInt(tags[i].style.maxWidth);
				tags[i].title="";
			}
		}
	}
}

//增加标签组,用于记忆标签点击顺序
function addtaggroup(id){
	if (id!="firstpage"){
		taggroup += ";"+id;
	}
}
//获取上一个标签,删除标签时使用
function getlastdiv(id){
	regExp = new RegExp(";"+id,"g");
	taggroup = taggroup.replace(regExp,"");
	var tags = taggroup.split(";");
	lastdiv = tags[tags.length-1];
	//alert(taggroup);
}

//创建document元素
function CreateEl(t,i,c){
	var x=document.createElement(t);
	x.id=i;
	x.className=c;
	x.style.display="block";
	return(x);
}
//获取浏览器信息
function getOs()
{
    if(navigator.userAgent.indexOf("MSIE 6.0")>0)return 1;
	if(navigator.userAgent.indexOf("MSIE 7.0")>0)return 2;
	if(isFirefox=navigator.userAgent.indexOf("Firefox")>0)return 3;
	if(isSafari=navigator.userAgent.indexOf("Safari")>0)return 4;
	if(isCamino=navigator.userAgent.indexOf("Camino")>0)return 5;
	if(isMozilla=navigator.userAgent.indexOf("Gecko/")>0)return 6;
	return 0;
}

var head = document.getElementsByTagName("head")[0];
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = "/javascripts/dep_manage/init.js";
head.appendChild(script);
